<?php
/*********************************************************************************
 *  PhpStorm - saas
 *-------------------------------------------------------------------------------
 * 版权所有: CopyRight By cw100.com
 * 文件内容简单说明
 *-------------------------------------------------------------------------------
 * $FILE:Qrcode.php
 * $Author:zxs
 * $Dtime:2017/7/4
 ***********************************************************************************/

namespace App\Http\Traits;

use App\Models\Maintain\Department;
use App\Models\Maintain\Departmentable;
use App\Models\Tmall\Qrcode;
use App\Models\Tools\PromptCoupons;

trait QrcodeTrait
{
    /**
     * @return array
     * 驱动存储
     */
    private function guards()
    {
        return [
            'store_goods' =>Departmentable::class,
            'store_activity' =>Departmentable::class,
            'coupon' => PromptCoupons::class,
            'store'=>Department::class,
            'qrcode' => Qrcode::class,
        ];
    }

    /**
     * @param $name
     * 获取模型驱动
     */
    private function getDriver($name)
    {
        return isset($this->guards()[$name]) ? $this->guards()[$name]:$name;
    }


    /**
     * @param $itemId
     * @param $name
     *
     *  返回相应数据
     */
    private function getData($model)
    {
        if ($model->item_type == 'card_vip') {
            return get_front_url($this->codeUrl($model->item_type), $model->tenant_id, 'weixin');
        }
        //        if ($model->item_type == 'store_goods') {
        //            return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id . '?store=' . $model->query, $model->tenant_id, 'weixin');
        //        }
        if ($model->item_type == 'store_goods') {
            $str = '';
            if(!empty($model->query)){
                $arr = explode(',', $model->query);
                if(isset($arr[0]) || isset($arr[1])){
                    $str .= '/' .$arr[0] . '/' . $arr[1];
                }
                if (isset($arr[2])) {
                    $str .= '&store=' . $arr[2];
                }
            }else{
                $str .= '/' . $model->item_id;
            }
            return get_front_url($this->codeUrl($model->item_type) . $str, $model->tenant_id, 'weixin');
        }
        if($model->item_type == 'turntableView') {
            return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id . '?preview=true', $model->tenant_id, 'weixin');
        }
        if($model->item_type == 'turntable') {
            return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id, $model->tenant_id, 'weixin');
        }
        if ($model->item_type == 'groupon') {
            return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id . '?store=' . $model->query, $model->tenant_id, 'weixin');
        }
        if ($model->item_type == 'goods' && !empty($model->query)) {
                return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id . '?skuid=' . $model->query, $model->tenant_id, 'weixin');
        }
        if ($model->item_type == 'store_activity' && !empty($model->query)) {
                return get_front_url($this->codeUrl($model->item_type) . '/' . $model->query . '?departmentable=' . $model->item_id, $model->tenant_id, 'weixin');
        }
        if($model->item_type == 'guide_bind'){
            return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id, $model->tenant_id, 'weixin');
        }
        return get_front_url($this->codeUrl($model->item_type) . '/' . $model->item_id, $model->tenant_id, 'weixin');
    }

    /**
     * @param $type
     * @return string
     * 生成对应的url地址
     */
    public function codeUrl($type)
    {
        switch ($type) {
        case 'store':
            $url = 'stores/show';
            break;
        case 'store_goods':
            $url = 'goods';
            break;
        case 'store_goods_sku':
            $url = 'goods';
            break;
        case 'store_activity':
            $url = 'stores/activity';
            break;
        case 'goods':
            $url = 'goods';
            break;
        case 'store_guide':
            $url = 'stores/guide';
            break;
        case 'guide_bind':
            $url = 'stores/guideBind';
            break;
        case 'coupon':
            $url = 'member/coupon/show';
            break;
        case 'card_vip':
            $url = 'member/card/index';
            break;
        case 'guide_list':
            $url = 'stores/guide/list';
            break;
        case 'subscribe':
            $url = 'subscribe/show';
            break;
        case 'turntableView':
            $url = 'turntable';
            break;
        case 'turntable':
            $url = 'turntable';
            break;
        case 'box':
            $url = 'box';
            break;
        case 'groupon':
            $url = 'goods/groupon';
            break;
        case 'poster':
            $url = 'poster';
            break;
        case 'share_partner':
            $url = 'poster/partner';
            break;
        }

        return $url;
    }
}
